package com.jt.tenant.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.jt.tenant.R;
import com.jt.tenant.adapter.ExpenseDetailsAdapter;
import com.jt.tenant.adapter.MaintainAdapter;
import com.jt.tenant.app.JTApplication;
import com.jt.tenant.conn.Get4SShopList;
import com.jt.tenant.conn.GetExpenseDetail;
import com.jt.tenant.popwindow.SortListPopup;
import com.jt.tenant.popwindow.YearListPopup;
import com.jt.tenant.widget.Tools;
import com.zcx.helper.http.AsyCallBack;
import com.zcx.helper.util.UtilToast;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class ExpenseDetailActivity extends JTActivity implements View.OnClickListener {

    private TextView mTitleText;

    private ListView mListView;

    private TextView cost;

    private ExpenseDetailsAdapter mAdapter;

    private LinearLayout mYearLin,mMonthLin;

    private TextView mTextYear,mTextMonth;

    private int flag = 0;

    public class YearModel{

        public String id;

        public String text;

        public String flag;

        public String type;

    }

    private List<YearModel> mYearLists = new ArrayList<>();

    private List<YearModel> mMonthList = new ArrayList<>();

    private YearListPopup mPop;

    private int year = 0;

    private int month = 0;

    private boolean isAll = false;

    private String selectedYear = "";

    private String selectedMonth = "";

    private double price = 0;

    private List<GetExpenseDetail.ExpenseDetailList> mList = new ArrayList<>();

    private GetExpenseDetail getExpenseDetail = new GetExpenseDetail(new AsyCallBack<GetExpenseDetail.Info>(){

        @Override
        public void onSuccess(String toast, int type, GetExpenseDetail.Info info) throws Exception {

            if(info.msg.equals("000")){

                mList.clear();

                mList.addAll(info.expenseDetailLists);

                if(mList.size()>0) {

                    price = 0;

                    for (int i = 0; i < mList.size(); i++){

                        price += Double.parseDouble(mList.get(i).price);

                    }

                    cost.setText(Tools.F_num(String.valueOf(price)));

                }else {

                    cost.setText("0元");

                }

                mAdapter.notifyDataSetChanged();

            }else {

                mList.clear();

                mAdapter.notifyDataSetChanged();

                cost.setText("0元");

            }
        }
    });

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_expense_detail);

        initView();

        initTime();

        getList();
    }

    private void initView(){

        mTitleText = (TextView) findViewById(R.id.tv_title_name);

        mTitleText.setText("消费明细");

        (mYearLin = (LinearLayout) findViewById(R.id.lin_year)).setOnClickListener(this);

        (mMonthLin = (LinearLayout) findViewById(R.id.lin_month)).setOnClickListener(this);

        cost = (TextView)findViewById(R.id.tv_expense_detail_cost);

        mTextYear = (TextView) findViewById(R.id.text_yaer);

        mTextMonth = (TextView) findViewById(R.id.text_month);

        mListView = (ListView) findViewById(R.id.list_expense_details);

        mAdapter = new ExpenseDetailsAdapter(this, mList);

        mListView.setAdapter(mAdapter);

    }

    private void initTime(){

     year = Integer.parseInt(Tools.getYear());

     month = Integer.parseInt(Tools.getMonth());

        YearModel yearModel;

        yearModel = new YearModel();

        yearModel.flag = "0";

        yearModel.text = "全部年份";

        yearModel.id = "-1";

        yearModel.type = "0";

        mYearLists.add(yearModel);

        for(int i=2016;i<year+1;i++){

            yearModel = new YearModel();

            yearModel.type = "0";

            yearModel.text = i + "";

            yearModel.id = i + "";

            if(i == year){
                yearModel.flag = "1";
            }
            else{
                yearModel.flag = "0";
            }

            mYearLists.add(yearModel);

        }

        yearModel = new YearModel();

        yearModel.flag = "0";

        yearModel.text = "全部月份";

        yearModel.id = "-2";

        yearModel.type = "1";

        mMonthList.add(yearModel);

        for(int i=1;i<month+1;i++){

            yearModel = new YearModel();

            yearModel.type = "1";

            yearModel.text = i + "";

            yearModel.id = i + "";

            if(i == month){
                yearModel.flag = "1";
            }
            else{
                yearModel.flag = "0";
            }

            mMonthList.add(yearModel);

        }

        selectedYear = year + "";

        if(month<10) {

            selectedMonth = "0"+month;

        }else {

            selectedMonth = month + "";

        }

        mTextYear.setText(year + "年");

        mTextMonth.setText(month + "月");

    }

    @Override
    public void onClick(View view) {

        switch (view.getId()){
            case R.id.lin_year:

                if(mPop!=null){

                    if(mPop.isShowing())

                        mPop.dismiss();

                    else{

                        if(flag==0){
                            mPop.showAsDropDown(view);
                        }

                        else{
                            mPop.changeList(mYearLists);

                            mPop.showAsDropDown(view);

                            flag=0;
                        }

                    }

                }else{

                    mPop = new YearListPopup(this,mYearLists);

                    initPopListener();

                    mPop.showAsDropDown(view);

                    flag=0;

                }

                break;

            case R.id.lin_month:

                if(!isAll){

                    if(mPop!=null){

                        if(mPop.isShowing())

                            mPop.dismiss();

                        else{

                            if(flag==1){
                                mPop.showAsDropDown(view);
                            }

                            else{
                                mPop.changeList(mMonthList);

                                mPop.showAsDropDown(view);

                                flag=1;
                            }

                        }

                    }else{

                        mPop = new YearListPopup(this,mMonthList);

                        initPopListener();

                        mPop.showAsDropDown(view);

                        flag=1;

                    }

                }

                break;


        }

    }
    private void initPopListener(){

        mPop.setOnItemClickListener(new YearListPopup.OnItemClick() {
            @Override
            public void onItemClick(YearModel model) {

                if(model.type.equals("0")){
                    //年
                    selectedMonth = "00";
                    if(model.id.equals("-1")){
                        //全部
                        isAll = true;
                        selectedYear = "0000";
                    }else{
                        isAll = false;
                        mMonthList.clear();
                        YearModel n_model = null;

                        n_model = new YearModel();

                        n_model.type="1";

                        n_model.id = "-2";

                        n_model.text = "全部月份";

                        n_model.flag = "1";

                        mMonthList.add(n_model);
                        if(model.id.equals(year+"")){
                            //点击当前年
                            for(int i=1;i<month + 1;i++){
                                n_model = new YearModel();

                                n_model.type="1";

                                n_model.id = i + "";

                                n_model.text = i+"";

                                n_model.flag = "0";

                                mMonthList.add(n_model);
                            }

                        }else{

                            for(int i=1;i<13;i++){
                                n_model = new YearModel();

                                n_model.type="1";

                                n_model.id = i + "";

                                n_model.text = i+"";

                                n_model.flag = "0";

                                mMonthList.add(n_model);
                            }

                        }
                        selectedYear = model.id;

                    }
                    if(model.id.equals("-1")){
                        mTextYear.setText(model.text);
                    }else{
                        mTextYear.setText(model.text + "年");
                    }
                    mTextMonth.setText("全部月份");
                }else{
                    //月
                    if(model.id.equals("-2")){
                        mTextMonth.setText(model.text);
                        selectedMonth = "00";
                    }
                    else{
                        mTextMonth.setText(model.text + "月");

                        if(Integer.parseInt(model.id)<10) {

                            selectedMonth = "0"+model.id;

                        }else {

                            selectedMonth = model.id;

                        }
                    }

                }

                changeSelectedId(model);

                getList();

            }
        });

    }

    private void changeSelectedId(YearModel model){

        if(model.type.equals("0")){
            //年
            for(int i=0;i<mYearLists.size();i++){
                if(mYearLists.get(i).id.equals(model.id)){
                    mYearLists.get(i).flag="1";
                }else{
                    mYearLists.get(i).flag="0";
                }
            }
        }else{
            //月
            for(int i=0;i<mMonthList.size();i++){
                if(mMonthList.get(i).id.equals(model.id)){
                    mMonthList.get(i).flag="1";
                }else{
                    mMonthList.get(i).flag="0";
                }
            }

        }


    }

    private void getList(){

        getExpenseDetail.date = selectedYear+"-"+selectedMonth;

        getExpenseDetail.execute(this);

    };





}
